<?php
//02. 获取百度关键词
/**
 * ring_news.state = 1 => 2
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
set_time_limit(180);
header("Content-type: text/html; charset=utf-8"); 

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(1);

$GLOBALS['config']['master']['db_database'] = 'mh_lnpan';
$GLOBALS['config']['slave']['db_database']  = 'mh_lnpan';

//$model = new Model('http://www.lnpan.com/index.php?m=api');
$model = new Db;
$re = 0;
$limit = 100;
while ($limit--) {
  $stop = $redis->get('LNPAN_QUEUE_STOP');
  if($stop) {
    sleep(3);
    die('stop');
  }
  
  $queue = $redis->lPop(TAG_PUSH_TO_QUEUE);
  //$queue = '84676@@不想这是场戏::张学友::粤语';
  if(!$queue) {
    echo2('tag_fetch_baidu queue empty');
    sleep(2);
    //$redis->lPush(TAG_FETCH_BAIDU_SUCCESS, $msg);
    continue;
  } 

  list($id, $list) = explode('@@', $queue);
  $list = explode('::', $list);
  $urls = array();
  $keywords = array();
  foreach ($list as $tag) {
    if(!strstr($tag, '歌曲') && mb_strlen($tag) < 8) {
      $tag .= ' 歌曲';
    }
    $rsp = rand(0, 9);
    $wd  = urlencode($tag);
    $oq  = urlencode(mb_substr($tag,0,2));

    $urls[] = 'http://www.baidu.com/s?wd='.$wd.'&rsp='.$rsp.'&f=1&oq='.$oq.'&ie=utf-8&rsv_ers=xn1&rs_src=0';
  }

  $results = curl_muliti_http($urls);
  foreach ($results as $result) {
    $html = $result['results'];
    $body = cut('<div id="rs">', '</table>', $html);
    preg_match_all('/<a[^>]*>(.+?)<\/a>/i', $body, $out);     

    if($out[1]) {
      $keywords = array_merge($keywords, $out[1]);
    }
  } //print_r($keywords);exit;

  foreach ($keywords as $keyword) {
    $keyword = trim($keyword);
    //标签库
    $ring_tag = $model->find("ring_tag", "name='$keyword'");
    if(empty($ring_tag)) {
      $data = array('name'=>$keyword);
      $ret = $model->add('ring_tag', $data);
      if(!$ret) {
        $msg = date('Y-m-d H:i:s').'::'.$id.'::'.$keyword;
        $redis->rPush(TAG_PUSH_TO_QUEUE, $queue);
        $redis->lPush(TAG_FETCH_BAIDU_FAILED, $msg);
        echo $queue;
        print_r($data);
        die('标签新增失败');
      } else {
        $tag_id = $model->insert_id();
      }
    } else {
      $tag_id = $ring_tag['id'];
    }

    //铃声标签关联库
    $ring_news_tag = $model->find('ring_news_tag', "news_id='$id' and tag_id='$tag_id'");
    if(empty($ring_news_tag)) {
      $data2 = array('news_id'=>$id, 'tag_id'=>$tag_id);
      $ret2 = $model->add('ring_news_tag', $data2);
      if(!$ret2) {
        $msg = date('Y-m-d H:i:s').'::'.$id.'::'.$keyword.'::'.$tag_id;
        $redis->rPush(TAG_PUSH_TO_QUEUE, $queue);
        $redis->lPush(TAG_FETCH_BAIDU_FAILED, $msg);
        echo $queue;
        print_r($data2);
        die('铃声标签关联库新增失败');
      }
    }
  }

  //铃声标签更新成功
  $msg = date('Y-m-d H:i:s').'::'.$id.'::'.implode(',', $keywords);
  $model->query("update ring_news set state=2 where id='$id'");
  //$model->close();
  $redis->lPush(TAG_FETCH_BAIDU_SUCCESS, $msg);

  print_r($id);
  print_r($list);
  print_r($urls);
  print_r($keywords);
  
  sleep(2);
}

